import type { AxiosProgressEvent } from "axios";
import axiosHttp from "../index";
import type { ModPermParams, NewFolderParams } from "./type";

export const getFileList = (id: string) => {
  return axiosHttp.get("/api/file/list", { params: { id } });
};

export const deleteFile = (id: string) => {
  return axiosHttp.get(`/api/file/delete?id=${id}`);
};

export const checkFile = (sha1: string) => {
  return axiosHttp.get(`/api/file/check?sha1=${sha1}`);
};

export const newFolder = (data: NewFolderParams) => {
  return axiosHttp.post("/api/file/createFolder", data);
};

export const downloadFile = (id: string) => {
  return axiosHttp.get(`/api/file/download?id=${id}`, {
    responseType: "blob",
  });
};

export const uploadFile = (
  data: FormData,
  onProgress?: (progressEvent: AxiosProgressEvent) => void,
) => {
  return axiosHttp.post("/api/file/upload", data, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
    onUploadProgress: onProgress,
  });
};

export const updateFile = (data: NewFolderParams) => {
  return axiosHttp.post("/api/file/update", data);
};

export const modPermFile = (data: ModPermParams) => {
  return axiosHttp.post("/api/file/modPerm", data);
};

export const shareFile = (id: string) => {
  return axiosHttp.get(`/api/file/share/${id}`);
};
